package dancrawler.client;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class ClientWorld implements Gameworld {

    private ArrayList<ArrayList<Tile>> tiles = new ArrayList<>();
    private int focus;



    public synchronized Tile getTile(int x, int y) {
        try {
            ArrayList<Tile> trow = tiles.get(x);
            return trow.get(y);
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
        
    }

    public synchronized void setTile(int x, int y, Tile tile) {
        while (tiles.size() <= x) {
            tiles.add(new ArrayList<Tile>());
        }
        ArrayList<Tile> trow = tiles.get(x);
        while (trow.size() <= y) {
            trow.add(new Tile(x, trow.size(), null, true));
        }
        trow.set(y, tile);
        tile.setX(x);
        tile.setY(y);
    }

    public synchronized  void setObjPos(GameObject obj, int x, int y) {
        if(obj == null)
        {
            return;
        }
        Tile t = getTile(obj.getX(), obj.getY());
        Tile t2 = getTile(x, y);
        if (t == null || t2 == null) {
        } else {
            t.remove(obj);
            t2.add(obj);
            obj.setX(x);
            obj.setY(y);
        }
    }

    public synchronized void addObj(GameObject obj, int x, int y) {
        Tile t = getTile(x, y);
        if (t == null) {
        } else {
            t.add(obj);
            obj.setX(x);
            obj.setY(y);
        }
    }
    




}
